package com.lw.yygh.order.controller.api;

import com.lw.yygh.enums.OrderStatusEnum;
import com.lw.yygh.enums.PaymentTypeEnum;
import com.lw.yygh.order.service.OrderService;
import com.lw.yygh.order.service.PaymentService;
import com.lw.yygh.order.service.WeixinService;
import com.lw.yygh.result.Result;
import org.apache.activemq.util.BooleanEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/order/weixin")
public class WeixinController {
    @Autowired
    private WeixinService weixinService;
    @Autowired
    private PaymentService paymentService;
    @Autowired
    private OrderService orderService;

    //生成微信支付扫描二维码
    @GetMapping("/createNative/{orderId}")
    public Result createNative(@PathVariable String orderId){
        Map<String,Object> map = weixinService.createNative(orderId);
        return Result.ok( map );
    }
    //查询支付状态
    @GetMapping("/queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable String orderId){
        //调用查询接口
        Map<String, String> resultMap = weixinService.queryPayStatus(orderId, PaymentTypeEnum.WEIXIN.name());
        if (resultMap == null) {//出错
            return Result.fail().message("支付出错");
        }
        if ("SUCCESS".equals(resultMap.get("trade_state"))) {//如果成功
            //更改订单状态，更新支付记录状态
            String out_trade_no = resultMap.get("out_trade_no");
            paymentService.paySuccess(out_trade_no, PaymentTypeEnum.WEIXIN.getStatus(), resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");

    }
    //取消预约
    @GetMapping("/auth/cancelOrder/{orderId}")
    public Result cancelOrder(@PathVariable String orderId){
        Boolean isOrder = orderService.cancelOrder(orderId);
        return Result.ok( isOrder );
    }

}
